#include "MonitoringRegister.h"
#include "ui_MonitoringRegister.h"
#include <QTime> 
#include <QJsonObject>
#include "SnaillTcpClient.h"
MonitoringRegister::MonitoringRegister(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MonitoringRegister),
    userSqlite(MonitoringUserSqlite::getInstance())
{
    ui->setupUi(this);
    timer=new QTimer(this);
    connect(timer, &QTimer::timeout, this,&MonitoringRegister::timerFinish);
    //注册
    connect(ui->btnRegister,&QPushButton::clicked,this,[=](){
        if(!MonitoringTool::isPhoneNumberValid(ui->lineEditUserName->text())){
            LOG_INFO("点击了注册");
            timer->start(3000);
            ui->label_2->setText("手机号格式不正确");
            return ;
        }
        if(ui->lineEditPassword->text().isEmpty()){
            timer->start(3000);
            ui->label_2->setText("密码不能为空");
            return ;
        }
        if(ui->lineEditRole->text().isEmpty()){
            timer->start(3000);
            ui->label_2->setText("角色不能为空");
            return ;
        }
        if (SnaillTcpClient::instance()->sendJsonData(MonitoringTool::getUserJson("regist",ui->lineEditUserName->text(),MonitoringTool::sha256Hash(ui->lineEditPassword->text()),ui->lineEditEmail->text(),ui->lineEditRole->text()))) {
            //  qDebug() << "Login data sent successfully";
        } else {
            ui->label_2->setText("登录失败，请检查网络");
            return ;
        }

    LOG_INFO("用户"+ui->lineEditUserName->text()+"注册了");
    });
    connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &MonitoringRegister::handleDataReceived);
    connect(ui->pushButton,&QPushButton::clicked,this,[=](){

        emit backClick();
    });
}
void MonitoringRegister::timerFinish()
{
    ui->label_2->clear();
}

void MonitoringRegister::handleDataReceived(const QByteArray &data)
{
    qDebug()<<"===="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    if(action=="regist"){
        QString coded = jsonObj["code"].toString();
        int code = coded.toInt();
        qDebug()<<"code=="<<code;
        if(code==0){
            timer->start(3000);
            ui->label_2->setText("用户已存在");
            return ;
        }
        if(code==3){
            timer->start(3000);
            ui->label_2->setText("数据库操作失败");
            return ;
        }

        emit registerClick();
    }
    //    if(userSqlite.insertData(ui->lineEditUserName->text(),ui->lineEditPassword->text(),ui->lineEditEmail->text(),ui->lineEditRole->text())==QUERY_SUCCESS_USER_EXISTS){
    //        timer->start(3000);
    //        ui->label_2->setText("用户已存在");
    //        return ;
    //    }
    //    if(userSqlite.insertData(ui->lineEditUserName->text(),ui->lineEditPassword->text(),ui->lineEditEmail->text(),ui->lineEditRole->text())==QUERY_FAILED){
    //        timer->start(3000);
    //        ui->label_2->setText("数据库操作失败");
    //        return ;
    //    }

}
MonitoringRegister::~MonitoringRegister()
{
    delete ui;
    delete timer;
}


